/*
*  Copyright 2019-2023 oyun
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/package com.oyun.modules.medicine.service.impl;

import com.oyun.modules.medicine.domain.Vaccine;
import FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oyun.modules.medicine.service.VaccineService;
import com.oyun.modules.medicine.domain.vo.VaccineQueryCriteria;
import com.oyun.modules.medicine.mapper.VaccineMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import PageResult;

/**
* @description 服务实现
* @author aoyun
* @date 2024-05-08
**/
@Service
@RequiredArgsConstructor
public class VaccineServiceImpl extends ServiceImpl<VaccineMapper, Vaccine> implements VaccineService {

    private final VaccineMapper vaccineMapper;

    @Override
    public PageResult<Vaccine> queryAll(VaccineQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(vaccineMapper.findAll(criteria, page));
    }

    @Override
    public List<Vaccine> queryAll(VaccineQueryCriteria criteria){
        return vaccineMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Vaccine resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Vaccine resources) {
        Vaccine vaccine = getById(resources.getId());
        vaccine.copy(resources);
        saveOrUpdate(vaccine);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<Vaccine> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Vaccine vaccine : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("蒙语疫苗名称", vaccine.getMnName());
            map.put(" mnNameCode",  vaccine.getMnNameCode());
            map.put(" zhName",  vaccine.getZhName());
            map.put("蒙语接种者", vaccine.getMnInoculator());
            map.put(" mnInoculatorCode",  vaccine.getMnInoculatorCode());
            map.put(" zhInoculator",  vaccine.getZhInoculator());
            map.put("蒙语接种方式", vaccine.getMnInoculationMethod());
            map.put(" mnInoculationMethodCode",  vaccine.getMnInoculationMethodCode());
            map.put(" zhInoculationMethod",  vaccine.getZhInoculationMethod());
            map.put("免疫期", vaccine.getImmunity period());
            map.put("免疫期单位（字典）", vaccine.getImmunityPeriodUnit());
            map.put("蒙语存储方式", vaccine.getMnStorageMethod());
            map.put(" mnStorageMethodCode",  vaccine.getMnStorageMethodCode());
            map.put(" zhStorageMethod",  vaccine.getZhStorageMethod());
            map.put("蒙语属性", vaccine.getMnStats());
            map.put(" zhStats",  vaccine.getZhStats());
            map.put(" mnStatsCode",  vaccine.getMnStatsCode());
            map.put(" createBy",  vaccine.getCreateBy());
            map.put(" createTime",  vaccine.getCreateTime());
            map.put(" updateBy",  vaccine.getUpdateBy());
            map.put(" updateTime",  vaccine.getUpdateTime());
            map.put("状态", vaccine.getStatus());
            map.put(" remark",  vaccine.getRemark());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}